package com.lzj.admin.controller;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author luoyoumou
 * WeChat lym520ccy
 * <p>
 * HelloController
 * @version 1.0
 * @date 2022-12-13 9:45
 */
@RestController
public class HelloController {

    @GetMapping("/hello")
    public static String hello() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetails) {
            return ((UserDetails) principal).getUsername();
        }

        if (principal instanceof Principal) {
            return ((Principal) principal).getName();
        }

        return "当前登录用户：" + String.valueOf(principal);
    }
}
